package com.gs.m_business_authority

import android.os.Bundle
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import com.gs.common.BaseCompatActivity
import com.gs.common.global.GlobalVariables
import com.gs.common.util.ActionBarUtil
import com.gs.common.util.DialogUtil
import com.gs.common.util.ToastUtil
import com.gs.m_business_authority.databinding.BusinessAuthorityLayoutBinding

class BusinessAuthorityActivity: BaseCompatActivity(), BusinessAuthorityPersistent.OnDoDealAuthorityStateCallback {

    private lateinit var binding: BusinessAuthorityLayoutBinding
    private val businessAuthorityPersistent by lazy { BusinessAuthorityPersistent.getInstance(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.business_authority_layout)

        // 设置自定义action bar
        ActionBarUtil.setCustomActionBar(this, R.layout.business_authority_actionbar_layout)

        // 设置返回键
        val backBtn = findViewById<ImageView>(R.id.back_btn)
        backBtn.setOnClickListener {
            finish()
        }

        val loginUser = GlobalVariables.loginUser
        if (loginUser == null) {
            ToastUtil.showShortToast(this, "用户登录状态已失效，请重新登录！")
            return
        }

        if (loginUser.isBusiness) {
            binding.businessImg.setImageDrawable(resources.getDrawable(com.gs.common.R.drawable.business_active))
            binding.businessAuthorityTv.text = "已申请"
            binding.businessAuthorityBtn.text = "立即注销"
        } else {
            binding.businessImg.setImageDrawable(resources.getDrawable(com.gs.common.R.drawable.business_inactive))
            binding.businessAuthorityTv.text = "未申请"
            binding.businessAuthorityBtn.text = "立即申请"
        }

        binding.businessAuthorityBtn.setOnClickListener {
            applyOrCancelBusinessAuthority(loginUser.isBusiness)
        }
    }

    private fun applyOrCancelBusinessAuthority(flag: Boolean) {
        val loginUser = GlobalVariables.loginUser
        if (loginUser == null) {
            ToastUtil.showShortToast(this, "申请或注销权限失败")
            return
        }
        if (flag) {
            // 向服务器发出注销权限api
            val alertDialog = DialogUtil.createCommonAlertDialog(this, "注销", "商家权限", "之前上架的商品会被全部清空哦！") {
                if (it == DialogUtil.CONFIRM_TO_DO) {
                    businessAuthorityPersistent.dealAuthority(loginUser.userName, false, this)
                }
            }
            alertDialog.show()
        } else {
            // 向服务器发出申请权限api
            val alertDialog = DialogUtil.createCommonAlertDialog(this, "申请", "商家权限") {
                if (it == DialogUtil.CONFIRM_TO_DO) {
                    businessAuthorityPersistent.dealAuthority(loginUser.userName, true, this)
                }
            }
            alertDialog.show()
        }
    }

    override fun onDealAuthoritySuccess(isBusiness: Boolean) {
        if (isBusiness) {
            binding.businessImg.setImageDrawable(resources.getDrawable(com.gs.common.R.drawable.business_active))
            binding.businessAuthorityTv.text = "已申请"
            binding.businessAuthorityBtn.text = "立即注销"
            ToastUtil.showShortToast(this, "申请权限成功")
        } else {
            binding.businessImg.setImageDrawable(resources.getDrawable(com.gs.common.R.drawable.business_inactive))
            binding.businessAuthorityTv.text = "未申请"
            binding.businessAuthorityBtn.text = "立即申请"
            ToastUtil.showShortToast(this, "注销权限成功")
        }
        GlobalVariables.loginUser?.isBusiness = isBusiness
    }

    override fun onDealAuthorityFailed(isBusiness: Boolean) {
        if (isBusiness) {
            ToastUtil.showShortToast(this, "申请权限失败")
        } else {
            ToastUtil.showShortToast(this, "注销权限失败")
        }
    }

}